package com.song.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.song.constant.MessageConstant;
import com.song.constant.PasswordConstant;
import com.song.constant.StatusConstant;
import com.song.context.BaseContext;
import com.song.dto.EmployeeLoginDTO;
import com.song.dto.EmployeePageQueryDTO;
import com.song.dto.EmployeeRegisterDTO;
import com.song.entity.Employee;
import com.song.exception.AccountLockedException;
import com.song.exception.AccountNotFoundException;
import com.song.exception.PasswordErrorException;
import com.song.mapper.EmployeeMapper;
import com.song.result.PageResult;
import com.song.service.EmployeeService;
import com.song.utils.Md5WithSalt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        //1. 根据用户名和密码查询员工
        Employee employee = employeeMapper.login(employeeLoginDTO);
        //2. 如果查询结果为空，抛出没有该用户的异常
        if (employee == null) throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        //3. 用户传入明文密码Md5加密后与数据库查询结果的用户密码进行比对

        //密码校验的结果
        boolean verifyPwdRes = Md5WithSalt.verifySalttedMd5(employee.getPassword(), employeeLoginDTO.getPassword());
        if(!verifyPwdRes) throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);

        //用户账号状态查询的结果 status = 1 表示正常，status = 0 表示禁用
        boolean accountStatus = Objects.equals(employee.getStatus(), StatusConstant.ENABLE);
        if (!accountStatus) throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);

        return employee;

    }

    @Override
    public void addEmployee(EmployeeRegisterDTO employeeRegisterDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeRegisterDTO,employee);
        //设置账号的初始状态
        employee.setStatus(StatusConstant.ENABLE);
        //明文密码初始值：123456,加密处理后存储在数据库
        employee.setPassword(Md5WithSalt.salttedMd5(PasswordConstant.DEFAULT_PASSWORD));
        //设置创建时间和更新时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //设置创建人和更新人
        employee.setCreateUser(BaseContext.getCurrentId());
        employee.setUpdateUser(BaseContext.getCurrentId());
        employeeMapper.addEmployee(employee);
    }
    //获取员工分页数据
    @Override
    public PageResult<Employee> getEmployeePage(EmployeePageQueryDTO employeePageQueryDTO) {

         //1.设置分页参数,拦截sql语句，插入分页参数
         PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
         //2.获取分页数据
         List<Employee> employeeList = employeeMapper.getEmployeePage(employeePageQueryDTO);
         //3.将分页数据封装成Page对象中
         Page<Employee> page = (Page<Employee>) employeeList;
         //4.返回最终的分页数据
         return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    public Integer updateEmployeeStatus(Integer status, Long id) {
      Employee employee =  Employee.builder()
              .status(status)
              .id(id).updateTime(LocalDateTime.now())
              .updateUser(BaseContext.getCurrentId())
              .build();
      return employeeMapper.updateEmployee(employee);
    }

    @Override
    public Employee getEmployeeById(Long id) {
      return employeeMapper.getEmployeeById(id);
    }

    @Override
    public Integer updateEmployee(Employee employee) {
       return employeeMapper.updateEmployee(employee);
    }
}
